<?php
namespace app\admin\controller;
use app\common\model;
use think\Exception;

class Manager extends Base {
    public $deviceModel;
    public function __construct () {
        parent::__construct();
    }
    public function index(){
        try{
            $datalist = db('manager')->order('id desc')->select();
            $this->assign('datalist',$datalist);
            return view();
        }catch (Exception $ex){
            $this->ajax(self::$codeError,'异常:'.$ex->getMessage());
            return false;
        }
    }
    
    public function addmanager(){
        try{
            $id = input('id');
            if($id) {
                $manager = db('manager')->where('id',$id)->find();
                $this->assign('manager',$manager);
            }
            if(request()->isPost()){
                $data = input();
                if($id) {
                    if(input('password')) {
                        if(!input('password2')) {
                            return returnJson(1, '请确认密码 ');
                        }
                        if(input('password')!=input('password2')) {
                            return returnJson(1, '两次密码不一致 ');
                        }
                        unset($data['password2']);
                        $data['password'] = md5($data['password'].config('SALT'));
                    }else{
                        unset($data['password']);
                        unset($data['password2']);
                    }
                    $res = db('manager')->where('mobile',$data['mobile'])->find();
                    if($res && $res['mobile']!=$manager['mobile']) {
                        return returnJson(1, '手机号已存在');
                    }
                    db('manager')->where('id',$id)->update($data);
                }else{
                    if(!input('mobile')) {
                        return returnJson(1, '请输入手机号');
                    }
                    if(!input('password')) {
                        return returnJson(1, '请输入密码 ');
                    }
                    if(!input('password2')) {
                        return returnJson(1, '请确认密码 ');
                    }
                    if(input('password')!=input('password2')) {
                        return returnJson(1, '两次密码不一致 ');
                    }
                    unset($data['password2']);
                    $data['password'] = md5($data['password'].config('SALT'));
                    $res = db('manager')->where('mobile',$data['mobile'])->find();
                    if($res) {
                        return returnJson(1, '手机号已存在');
                    }
                    $data['create_time'] = date('Y-m-d H:i:s');
                    $id = db('manager')->insertGetId($data);
                }
                
                return returnJson(0,'保存成功');
            }

            $where = "(manager_id='' or manager_id is null or manager_id=0)";

            if($id) {
                $where .= " or manager_id='{$id}'";
            }
     

            $rolelist = db('role')->select();
            $this->assign('rolelist',$rolelist);
            $this->view->engine->layout('common/login-layout');
            return view();
        }catch (Exception $ex){
            $this->ajax(self::$codeError,'异常:'.$ex->getMessage());
            return false;
        }
    }
    
    public function deletemanager () {
        try{
            if(!request()->isAjax()){
                return returnJson(1,'error');
            }
            $id = input('id');
            if(!$id) {
                return ajaxReturn(1,'error');
            }
            $manager = db('manager')->where('id',$id)->find();
            $res = db('manager')->where(['id'=>$id])->delete();
            return returnJson(0,'删除成功');
        }catch (Exception $ex){
            $this->ajax(self::$codeError,'异常:'.$ex->getMessage());
            return false;
        }
    }
    
    public function role () {
        try{
            $datalist = db('role')->order('id desc')->select();
            $this->assign('datalist',$datalist);
            return view();
        }catch (Exception $ex){
            $this->ajax(self::$codeError,'异常:'.$ex->getMessage());
            return false;
        }
    }
    
    public function addrole () {
        try{
            $id = input('id');

            if(request()->isPost()){
                if(!input('name')) {
                    return returnJson(1, '请输入名称');
                }
                $data = input();
                $data['menu'] = implode(',', $data['menu']);
                if($id) {
                    db('role')->where('id',$id)->update($data);
                    return returnJson(0,'修改成功','','/admin/manager/role');
                }else{
                    $data['create_time'] = date('Y-m-d H:i:s');
                    db('role')->insert($data);
                    return returnJson(0,'添加成功','','/admin/manager/role');
                }
            }

            if($id) {
                $role = db('role')->where('id',$id)->find();
                $this->assign('role',$role);
                $menuArr = explode(',',$role['menu']);
            }
            $menuwhere = [
                'status' => 1,
                'level' => 1
            ];

            $menus = db('menu')->where($menuwhere)->select()->toArray();
            foreach ($menus as $k=>$menu) {
                $menus[$k]['checked'] = 0;
                if(in_array($menu['id'], $menuArr)) {
                    $menus[$k]['checked'] = 1;
                }
                $subwhere = [
                    'pid' => $menu['id'],
                    'status' => 1,
                    'level' => array('neq',1)
                ];
                $menus[$k]['menus'] = db('menu')->where($subwhere)->select()->toArray();
                foreach ($menus[$k]['menus'] as $kk=>$vv) {
                    $menus[$k]['menus'][$kk]['checked'] = 0;
                    if(in_array($vv['id'], $menuArr)) {
                        $menus[$k]['menus'][$kk]['checked'] = 1;
                    }
                }
            }
            $this->assign('menus', json_encode($menus));

            $this->view->engine->layout('common/login-layout');
            return view();
        }catch (Exception $ex){
            $this->ajax(self::$codeError,'异常:'.$ex->getMessage());
            return false;
        }
    }
    
    public function deleterole() {
        try{
            if(!request()->isAjax()){
                return returnJson(1,'error');
            }
            $id = input('id');
            if(!$id) {
                return ajaxReturn(1,'error');
            }
            $role = db('role')->where('id',$id)->find();
            if(!$role) {
                return ajaxReturn(1,'error');
            }
            $res = db('role')->where(['id'=>$id])->delete();
            return returnJson(0,'删除成功');
        }catch (Exception $ex){
            $this->ajax(self::$codeError,'异常:'.$ex->getMessage());
            return false;
        }
    }
}
